package com.anche.demo.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.anche.demo.entity.User;
import com.anche.demo.service.impl.UserService;
import com.anche.demo.util.HttpDistribute;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2022/11/27 11:58
 * @Created by Administrator
 */
@RestController
@RequestMapping("")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("save")
    public String save(@RequestBody User user){
        userService.save( user);
        return "ok";
    }
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        userService.upload(file);
        return "success";
    }
    @PostMapping("export")
    @ResponseBody
    public void export(HttpServletResponse response) throws IOException {
        List<User> dataList = userService.list();

        String fileName = "data.xlsx";

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .build();

        WriteSheet writeSheet = EasyExcel.writerSheet(0)
                .head(User.class)
                .build();

        excelWriter.write(dataList, writeSheet);
        excelWriter.finish();
    }
    @PostMapping("insert")
    public String insert(@RequestBody User user) {
//        userService.insert(user);
        return "ok";
    }

    @GetMapping("/user/get")
    public String get(HttpServletRequest httpServletRequest, @RequestParam("name") String name, @RequestParam("_w_signature") String signature) throws Exception {
        String domain = httpServletRequest.getHeader("domain");
        HttpDistribute.distributionByGet(new HashMap<>(), domain);
        log.info("name{}", name);
        log.info("_w_signature{}", signature);
        return "ok";
    }

    @GetMapping("/haha/user/get")
    public String test(HttpServletRequest httpServletRequest, @RequestParam("name") String name, @RequestParam("_w_signature") String signature) throws Exception {
        log.info("转发成功");
        return "ok";
    }

    @GetMapping("/haha/user/data")
    public String data() throws Exception {
        hhh();
        return "";
    }

    public static void hhh(){
    }


    @PostMapping(value = "/convert")
    @ApiOperation(value = "上传文件")
    public String convert() {
        userService.httpConvert();
        return "";
    }
}